package com.atguigu.jxc.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.CustomerReturnListGoodsMapper;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import org.apache.shiro.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {

    @Autowired
    UserDao userDao;

    @Autowired
    CustomerReturnListGoodsMapper customerReturnListGoodsMapper;

    /**
     * 客户退货保存
     * @param customerReturnList
     * @param customerReturnListGoodsStr
     * @return
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @Override
    public ServiceVO save(CustomerReturnList customerReturnList, String customerReturnListGoodsStr) {

        //将Json数据转化成List<ServiceVo>
        List<CustomerReturnListGoods> voList =  JSONObject.parseArray(customerReturnListGoodsStr,CustomerReturnListGoods.class);

        //用户信息
        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        customerReturnList.setUserId(user.getUserId());

        //保存退货商品
        customerReturnListGoodsMapper.save(customerReturnList);

        //保存退货商品原因
        for (CustomerReturnListGoods customerReturnListGoods : voList) {
            customerReturnListGoodsMapper.insert(customerReturnListGoods);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);


    }

    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<Map> list = customerReturnListGoodsMapper.count(sTime, eTime, goodsTypeId, codeOrName);
        return JSON.toJSONString(list);
    }
}
